package com.ylq.controller;

import com.ylq.entity.PageVo;
import com.ylq.entity.Result;
import com.ylq.entity.Roles;
import com.ylq.service.RolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author qiang
 * @Description 角色类的控制层
 * @Date 2020-04-27
 **/
@RestController
@Api(description = "【角色】相关的增删改查操作")
@RequestMapping("/api")
public class RolesController {
    @Autowired
    private RolesService rolesService;
    Roles roles =  new Roles();

    @ApiOperation("添加角色")
    @PostMapping("/CreateRole")
    public Object createRole(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token, @ApiParam("角色名")@RequestParam("name") String name){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            roles.setRoleNames(name);
            int i = rolesService.createRole(roles);
            if (i>0){
                return new Result(roles.getUid(),"",true);
            }else {
                return new Result("添加失败","",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    @ApiOperation("修改角色")
    @PostMapping("/UpdateRole")
    public Object updateRole(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("角色名")@RequestParam("name") String name,@ApiParam("ID")@RequestParam("roleId") String roleId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            roles.setRoleNames(name);
            roles.setUid(roleId);
            int i = rolesService.updateRole(roles);
            if (i>0){
                return new Result("修改成功","",true);
            }else {
                return new Result("修改失败","",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    @ApiOperation("分页查询角色信息")
    @PostMapping("GetRolesLayUI")
    public Object getRolesLayUI(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("当前第几页")@RequestParam("page") Integer page,@ApiParam("每页数据条数")@RequestParam("limit")  Integer limit){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            PageVo pageVo = new PageVo();
            pageVo.setPage(page);
            pageVo.setPageSize(limit);
            List<Roles> ui =  rolesService.getRolesLayUI(page,limit);
            int i = rolesService.countAllRoles();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code",0);
            map.put("msg","");
            map.put("count",i);
            map.put("data",ui);
            return map;
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    @ApiOperation("删除角色信息")
    @PostMapping("DeleteRole")
    public Object deleteRole(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("角色id")@RequestParam("roleId") String roleId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            int i = rolesService.deleteRole(roleId);
            rolesService.deleteRm(roleId);
            if (i>0){
                return new Result("","删除成功",true);
            }else{
                return new Result("","删除失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    @ApiOperation("获取所有角色")
    @PostMapping("GetRolesAll")
    public Object getRolesAll(){
       List<Roles> list = rolesService.getRolesAll();
       return  list;
    }
    @ApiOperation("获取指定用户的所有角色信息")
    @PostMapping("GetRolesByUserIdLayUI")
    public Object getRolesByUserIdLayUI(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("用户id")@RequestParam("uId") String uId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            List<Roles> list = rolesService.getRolesByUserIdLayUI(uId);
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("code",0);
            map.put("msg","");
            map.put("count",list.size());
            map.put("data",list);
            return  map;
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
}
